package com.fragrance.model;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fragrance.util.Constants;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * 设备绑定信息表 t_user_device_bind
 * 
 * @author wangyuankun
 *
 */
public class UserDeviceBindModel extends Model<UserDeviceBindModel>{
	
	private final static Logger logger = LoggerFactory.getLogger(UserDeviceBindModel.class);

	private static final long serialVersionUID = 4976951028620631370L;

	public final static UserDeviceBindModel me = new UserDeviceBindModel();
	
	/**
	 * 根据imei号查询设备
	 * 
	 * @params imei
	 */
	public List<UserDeviceBindModel> findBindDevice(String imei){
		return find("select * from t_user_device_bind where imei = ? and status = 'N' ", imei);
	}
	
	/**
	 * 微信绑定设备
	 * @param data
	 * @return
	 */
	public Boolean bindCarlaToUser(Map<String, Object> data) {
		boolean flag =false;
		try {
			
			 flag = new UserDeviceBindModel().set("wx_open_id", data.get("openid"))//
					.set("imei", data.get("imei"))//
					.set("create_id", data.get("openid"))
					.set("create_date", new Date()).save();
		} catch (Exception e) {
			logger.error("用户绑定设备失败", e);
		}
		return flag;
	}

	public UserDeviceBindModel findBindDeviceInfo(Map<String, Object> data) {
		String imei = (String) data.get("imei");
		String openId = (String) data.get("openid");
		List<UserDeviceBindModel> userDeviceBindModels = 
				find("select * from t_user_device_bind where imei = ? and wx_open_id = ? and status = 'N' ", imei,openId);
		if (userDeviceBindModels != null && userDeviceBindModels.size() == 1) {
			return userDeviceBindModels.get(0);
		}
		 return null;
	}

	public  boolean unBindCarla(Map<String, Object> data) {
		String imei = (String) data.get("imei");
		String openId = (String) data.get("openid");
		try {
			return this. 
					find("select * from t_user_device_bind where imei = ? and wx_open_id = ? and status = 'N' ", imei,openId)
					.get(0)
					.set("update_id", openId)
					.set("update_date", new Date())
					.set("status", "D")
					.update();
		} catch (Exception e) {
			logger.error("解绑机器状态失败", e);
		}
		return false;
	}
	
	//获取绑定设备列表
	public List<UserDeviceBindModel> getDeviceBindList(String openid) {
		return this.find("select * from t_user_device_bind where wx_open_id = ?  and status = 'N'", openid);
	}
	
	// 设备绑定列表
	public Map<String, Object> deviceBindList(Map<String, Object> params) {
		logger.info("---- begin  UserDeviceBindModel deviceBindList --- params{}", params);
		String imei = (String) params.get("IMEI");
		String status = "N";
		String pageNum = params.get("pageNum") == null ? "1" : params.get("pageNum").toString();
		String pageSize = params.get("pageSize") == null ? "20" : params.get("pageSize").toString();
		StringBuffer sql = new StringBuffer("FROM t_user_device_bind where 1 = 1");
		Map<String, Object> resultMap = new HashMap<String, Object>();

		Map<String, Object> devicePageMap = new HashMap<String, Object>();
		try {
			if (!StringUtils.isBlank(imei)) {
				sql.append(" and imei = '" + imei + "'");
			}
			if (!StringUtils.isBlank(status)) {
				sql.append(" and status = '" + status + "'");
			}
			logger.info("---- info  UserDeviceBindModel deviceBindList --- sql{}", sql);
			Page<UserDeviceBindModel> deviceBindPage = UserDeviceBindModel.me.paginate(Integer.parseInt(pageNum),
					Integer.parseInt(pageSize), "SELECT *", sql.toString());
			logger.info("---- info  UserDeviceBindModel deviceBindList --- deviceList{}", deviceBindPage.getList());
			devicePageMap.put("deviceList", deviceBindPage.getList());
			devicePageMap.put("totalPage", deviceBindPage.getTotalPage());
			devicePageMap.put("pageNum", deviceBindPage.getPageNumber());
			devicePageMap.put("pageSize", deviceBindPage.getPageSize());
			devicePageMap.put("totalRow", deviceBindPage.getTotalRow());
			logger.info("---- info  UserDeviceBindModel deviceBindList --- devicePageMap{}", devicePageMap);
			resultMap.put("code", Constants.SUCCESS_CODE);
			resultMap.put("msg", Constants.SUCCESS_MSG);
			resultMap.put("data", devicePageMap);
		} catch (Exception e) {
			logger.error("-- error UserDeviceBindModel.deviceBindList,Exception{}", e);
			resultMap.put("code", "30101");
			resultMap.put("msg", "获取绑定设备列表失败");
		}
		return resultMap;
	}

	public Map<String, Object> deviceBindUntie(Map<String, Object> params) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		String imei = (String) params.get("IMEI");
		try {
//			
			int flag = Db.delete("delete from t_user_device_bind where imei = ? ", imei);
			if(flag > 0){
				resultMap.put("code", Constants.SUCCESS_CODE);
				resultMap.put("msg", Constants.SUCCESS_MSG);
			}
		} catch (Exception e) {
			logger.error("后台解绑机器状态失败", e);
			resultMap.put("code", "300102");
			resultMap.put("msg", "后台解绑设备失败");
		}
		return resultMap;
	}

}
